using System;
using System.Collections.Generic;
using UnityEngine;

namespace ai_library
{	
	public class OpportunityBuilderException : System.Exception
	{
		protected string message;
		public OpportunityBuilderException(string message) { this.message = message; }
		public override string Message { get { return message; } }
		public override string ToString() { return "Opportunity Builder Exception: " + message; }
	}

	public static class OpportunityBuilder<T> where T : IEntity, IEntityStateMachine<T>
	{
		public static IOpportunity<T> Build(ConditionTypes conditionType, params ActionTypes[] actionTypes)
		{
			try
			{
				ICondition<T> condition = ConditionFactory<T>.Get(conditionType);
			
				List<ActionTypes> actionTypeList = new List<ActionTypes>(actionTypes);

				List<IAction<T>> actions = new List<IAction<T>>();
				
				foreach (ActionTypes a in actionTypeList)
				{
					IAction<T> builtAction = ActionFactory<T>.Get(a);

					if (builtAction == null)
						throw new OpportunityBuilderException("Failed to add Action");
					else
						actions.Add(builtAction);
						
				}

				return new Opportunity<T>(condition, actions);
			}
			catch (ConditionFactoryException e)
			{
				throw new OpportunityBuilderException(e.ToString());
			}
			catch (ActionFactoryException e)
			{
				throw new OpportunityBuilderException(e.ToString());
			}
		}
	}
}

